﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System.IO;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
using Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.SB.Bundling;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.SB.Localization.SBTheme;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.SB.Navigation;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.SB.Toolbars;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Bundling;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars;
using Volo.Abp.AspNetCore.Mvc.UI.Theming;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.UI.Navigation;
using Volo.Abp.VirtualFileSystem;

namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.SB
{
    [DependsOn(
        typeof(AbpAspNetCoreMvcUiThemeSharedModule),
        typeof(AbpAspNetCoreMvcUiMultiTenancyModule)
        )]
    public class AbpAspNetCoreMvcUISBThemeModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var hostingEnvironment = context.Services.GetHostingEnvironment();

            Configure<NavigationOptions>(options =>
            {
                options.MenuContributors.Add(new SBMenuContributor());
            });


            Configure<ThemingOptions>(options =>
            {
                options.Themes.Add<SBTheme>();

                if (options.DefaultThemeName == null)
                {
                    options.DefaultThemeName = SBTheme.Name;
                }
            });

            Configure<AbpLocalizationOptions>(options =>
            {
                options.Resources
                    .Add<SBThemeResource>("en")
                    .AddVirtualJson("/Localization/SBTheme");
            });


            Configure<VirtualFileSystemOptions>(options =>
            {
                options.FileSets.AddEmbedded<AbpAspNetCoreMvcUISBThemeModule>("Volo.Abp.AspNetCore.Mvc.UI.Theme.SB");
            });

            if (hostingEnvironment.IsDevelopment())
            {
                Configure<VirtualFileSystemOptions>(options =>
                {
                    options.FileSets.ReplaceEmbeddedByPhysical<AbpAspNetCoreMvcUISBThemeModule>(Path.Combine(hostingEnvironment.ContentRootPath, string.Format("..{0}Volo.Abp.AspNetCore.Mvc.UI.Theme.SB", Path.DirectorySeparatorChar)));
                });
            }

            Configure<ToolbarOptions>(options =>
            {
                options.Contributors.Add(new SBThemeMainTopToolbarContributor());
            });

            Configure<BundlingOptions>(options =>
            {
                options
                    .StyleBundles
                    .Add(SBThemeBundles.Styles.Global, bundle =>
                    {
                        bundle
                            .AddBaseBundles(StandardBundles.Styles.Global)
                            .AddContributors(typeof(SBThemeGlobalStyleContributor));
                    });

                options
                    .ScriptBundles
                    .Add(SBThemeBundles.Scripts.Global, bundle =>
                    {
                        bundle
                            .AddBaseBundles(StandardBundles.Scripts.Global)
                            .AddContributors(typeof(SBThemeGlobalScriptContributor));
                    });
            });
        }
    }
}
